<template>
  <div class="ele-body">
    <el-card shadow="never">

      <el-popover ref="search" placement="bottom" @hide="clearForm">
        <template >
          <!-- 搜索表单 -->
          <el-form :model="table.where" label-width="77px" class="ele-form-search"
                   @keyup.enter.native="$refs.studentTable.reload()" @submit.native.prevent>
                <el-form-item label="学生姓名:">
                  <el-input v-model="table.where.name" placeholder="请输入学生姓名" clearable/>
                </el-form-item>

            <el-form-item label="学号:">
              <el-input v-model="table.where.name" placeholder="请输入学号" clearable/>
            </el-form-item>

                <el-form-item label="性别：" prop="gender">
                  <el-select v-model="table.where.gender" placeholder="请选择性别" class="ele-block" clearable>
                    <el-option label="男" :value="1"/>
                    <el-option label="女" :value="2"/>
                    <el-option label="未知" :value="3"/>
                  </el-select>
                </el-form-item>

                <el-form-item label="学生状态:">
                  <el-input v-model="table.where.study_status" placeholder="请输入学生状态" clearable/>
                </el-form-item>

                <el-form-item>
                  <div class="ele-form-actions">
                    <el-button type="primary" @click="$refs.studentTable.reload()" icon="el-icon-search" class="ele-btn-icon">查询
                    </el-button>
                    <el-button @click="(table.where={})&&$refs.studentTable.reload()">重置</el-button>
                  </div>
                </el-form-item>

          </el-form>
        </template>
      </el-popover>

      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button v-popover:search type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">查询</el-button>
        <el-button @click="showAddStudentForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">添加学生</el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small">批量删除</el-button>
        <el-button @click="showCreateDataImportForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">批量入库</el-button>
        <el-button @click="showDataExportForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">导出</el-button>
        <el-button @click="showUpdateDataImportForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">数据更新</el-button>
        <el-button @click="showBatchBindCardForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">批量绑卡</el-button>
      </div>

      <!-- 数据表格 -->
      <ele-data-table ref="studentTable" :config="table" :choose.sync="choose" height="calc(100vh - 315px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" show-overflow-tooltip/>
                  
          <el-table-column prop="class_id" label="照片" sortable="custom" show-overflow-tooltip min-width="120">
            <template slot-scope="{row}">
              <el-image
                  style="width: 100px; height: 100px"
                  :src="row.face_url"
                  :preview-src-list="row.origin_face_url">
              </el-image>
            </template>
          </el-table-column>

          <el-table-column prop="face_check" label="人脸检测" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="name" label="学生姓名" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="gender" label="性别" sortable="custom" show-overflow-tooltip min-width="80"/>

          <el-table-column prop="class_name" label="所属班级" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="dept_name" label="学部" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="number" label="学号" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="ic_card" label="考勤卡号" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="accommodation_type_name" label="住宿类型" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="parents" label="关联监护人" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="study_status_name" label="学生状态" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="" label="小程序绑定状态" sortable="custom" show-overflow-tooltip min-width="150"/>

          <el-table-column prop="" label="订阅状态" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false">修改</el-link>
              <el-popconfirm title="确定要删除此学生吗？" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>

    </el-card>

    <add-student :showAddStudentForm.sync="showAddStudentForm" @reload="this.$refs.table.reload()"></add-student>
    <create-data-import :showCreateDataImportForm.sync="showCreateDataImportForm"></create-data-import>
    <data-export :showDataExportForm.sync="showDataExportForm"></data-export>
    <update-data-import :showUpdateDataImportForm.sync="showUpdateDataImportForm"></update-data-import>
    <batch-bind-card :showBatchBindCardForm.sync="showBatchBindCardForm"></batch-bind-card>

  </div>
</template>

<script>
import AddStudent from './create';
import CreateDataImport from './create-data-import';
import UpdateDataImport from './update-data-import';
import BatchBindCard from './batch-bind-card';
import DataExport from './data-export';
export default {
  name: "SchoolStudent",
  components: {AddStudent, CreateDataImport, DataExport, UpdateDataImport, BatchBindCard},
  data() {
    return {
      table: {url: '/student/list', where: {}},  // 表格配置
      choose: [],  // 表格选中数据
      showEdit: false,  // 是否显示表单弹窗
      showAddStudentForm: false, // 是否显示创建学生表单弹窗
      showCreateDataImportForm: false, // 是否显示创建数据导入弹窗
      showDataExportForm: false, // 是否显示数据导出弹窗
      showUpdateDataImportForm: false, // 是否显示更新数据导入弹窗
      showBatchBindCardForm: false, // 是否显示批量绑卡弹窗
      editForm: {},  // 表单数据
      searchForm: {}, // 搜索表单
      editRules: {  // 表单验证规则
              
        class_id: [
          {required: true, message: '请输入班级ID', trigger: 'blur'}
        ],
                                    
        name: [
          {required: true, message: '请输入学生姓名', trigger: 'blur'}
        ],
                          
        gender: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
                                    
        number: [
          {required: true, message: '请输入学号', trigger: 'blur'}
        ],
                          
        study_status: [
          {required: true, message: '请选择学生状态', trigger: 'blur'}
        ],
                          
        birthday: [
          {required: true, message: '请输入生日', trigger: 'blur'}
        ],
                          
        idcard_type: [
          {required: true, message: '请选择证件类型', trigger: 'blur'}
        ],
                                    
        idcard: [
          {required: true, message: '请输入证件号', trigger: 'blur'}
        ],
                                    
        accommodation_type: [
          {required: true, message: '请输入数据字典中学生住宿类型编码', trigger: 'blur'}
        ],
                          
        census_register: [
          {required: true, message: '请输入户籍', trigger: 'blur'}
        ],
                                    
        address: [
          {required: true, message: '请输入住址', trigger: 'blur'}
        ],
                                    
        ic_card1: [
          {required: true, message: '请输入ic卡号1', trigger: 'blur'}
        ],
                                    
        ic_card2: [
          {required: true, message: '请输入ic卡号2', trigger: 'blur'}
        ],
                                    
        ic_card3: [
          {required: true, message: '请输入ic卡号3', trigger: 'blur'}
        ],
                          
        sort: [
          {required: true, message: '请输入排序', trigger: 'blur'}
        ],
              
      },
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 显示编辑
     */
    edit(row) {
      this.$http.get('/student/info?id=' + row.id).then(res => {
        this.editForm = res.data;
        this.showEdit = true;
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 保存编辑
     */
    save() {
      this.$refs['editRuleForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({lock: true});
          this.$http.post('/student/edit', this.editForm).then(res => {
            loading.close();
            if (res.data.code === 200) {
              this.showEdit = false;
              this.$message({type: 'success', message: res.data.msg});
              this.$refs.studentTable.reload()
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 刪除(批量刪除)
     */
    remove(row) {
      console.log(row)
      if (!row) {  // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
        let ids = this.choose.map(d => d.id);
        this.$confirm('确定要删除选中的学生吗?', '提示', {type: 'warning'}).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/student/destroy', {id: ids}).then(res => {
            loading.close();
            this.$message({type: 'success', message: res.msg});
            this.$refs.studentTable.reload()
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => 0);
      } else {  // 单个删除
        const loading = this.$loading({lock: true});
        this.$http.post('/student/destroy', {id:row.id}).then(res => {
          loading.close();
          this.$message({type: 'success', message: res.msg});
          this.$refs.studentTable.reload()
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }
    },
    /**
     * 清理查询表单
     */
    clearForm() {
      this.searchForm = {};
    }                                                         }
}
</script>

<style scoped>
.ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
  width: 100%;
}
</style>